package com.sy.simpleegg.platform.lyysdk

import com.sy.simpleegg.ext.logFile
import com.sy.simpleegg.util.GsonUtil.getInstance
import com.sy.nettyclient.client.NettyClient
import com.sy.nettyclient.client.NettyClientProperties
import com.sy.nettyclient.common.constant.ClientConstants
import com.sy.nettyclient.common.param.CommonUploadCoinsParam
import com.sy.nettyclient.common.param.GetPayCodeParam
import com.sy.nettyclient.common.param.GetRuleParam
import com.sy.nettyclient.common.param.ICCIDParam
import com.sy.nettyclient.common.param.ReplyStartEqParam
import com.sy.nettyclient.common.param.ReportReversePayInfoParam
import com.sy.nettyclient.msg.MsgProducer
import com.sy.nettyclient.msg.handler.AbstractLogHandler
import com.sy.nettyclient.msg.handler.MsgHandler
import java.util.UUID

class LyySdkClient (
    appid: String?,
    appSecret: String?,
    host: String?,
    port: Int,
    uuid: String?,
    devType: String?,
    loginFlag: String?,
    msgHandler: MsgHandler?
): NettyClient(appid, appSecret, host, port, uuid, devType, loginFlag, msgHandler){

    private val TAG = "LyySdkClient"

    private var nettyClient: NettyClient? = null

    var tempKey: String? = null

    init {

    }

//    fun lyySdkClient(
//        appid: String?,
//        appSecret: String?,
//        host: String?,
//        port: Int,
//        uuid: String?,
//        devType: String?,
//        loginFlag: String?,
//        handler: MsgHandler?
//    ) {
//        try {
//            nettyClient =
//                NettyClient(appid, appSecret, host, port, uuid, devType, loginFlag, handler)
//            nettyClient!!.logLevel(ClientConstants.LogLevel.DEBUG)
//            nettyClient!!.logHandler(MyLogHandler())
//            nettyClient!!.connect()
//        } catch (e: Exception) {
//            e.printStackTrace()
//            Logger.e("初始化LyyClient异常：" + e.message)
//        }
//    }

    fun getRule(): Boolean {
        val param = GetRuleParam()
        param.uniqueKey = UUID.randomUUID().toString()
        return MsgProducer.getRule(param)
    }

    fun getPayCode(pkgData: String, payType: Int, k: String): Boolean {
        println("获取支付二维码......$k")
        val param = GetPayCodeParam()
        param.uniqueKey = k
        val channels: MutableList<String> = ArrayList()
        channels.add(pkgData)
        param.selectChannels = channels
        param.isGaming = false
        param.payType = payType
        return MsgProducer.getPayCode(param)
    }

    fun replyCheckEq(success: Int, faileMsg: String?): Boolean {
//        ReplyCheckEqParam param = new ReplyCheckEqParam();
//        param.setData(success);
//        param.setFailure(faileMsg);
//        return MsgProducer.replyCheckEq(param);
        return true
    }

    fun replyStartEq(success: Int, faileMsg: String?, k: String?): Boolean {
        val startEqParam = ReplyStartEqParam()
        startEqParam.uniqueCode = k
        startEqParam.data = success
        if (success == 1) {
            startEqParam.failure = faileMsg
        }
        return MsgProducer.replyStartEq(startEqParam)
    }

    fun uploadEqStartResult(d: String, k: String?, msg: String?): Boolean {
        val param = ReplyStartEqParam()
        val data = d.toInt()
        if (data == 1) {
            param.failure = msg
        }
        param.data = data
        param.uniqueCode = k
        logFile("启动结果上报：" + getInstance()!!.toJson(param))
        return MsgProducer.uploadEqStartResult(param)
    }

    fun uploadCommonCoins(total: String?, count: String?): Boolean {
        println("上传投币信息......")
        val key = UUID.randomUUID().toString()
        val param = CommonUploadCoinsParam()
        param.uniqueCode = key
        param.total = total
        param.count = count
        //        Logger.d("上报投币数据：" + GsonUtil.getInstance().toJson(param));
        return MsgProducer.uploadCommonCoins(param)
    }


    fun iccidReport(iccid: String): Boolean {
        println("上传iccid。。。。$iccid")
        val param = ICCIDParam()
        param.iccid = iccid
        param.uniqueCode = uuid()
        return MsgProducer.reportICCID(param)
    }

    fun postBackscanPay(k: String, pkgData: String, qrCode: String): Boolean{
        val param = ReportReversePayInfoParam()
        param.uniqueKey = k
        val goods: MutableList<String> = mutableListOf()
        goods.add(pkgData)
        param.qrCode = qrCode
        param.isGaming = false
        return MsgProducer.reportReversePayInfo(param)
    }

//    public static boolean reportReversePayInfo(){
//        ReportReversePayInfoParam param = new ReportReversePayInfoParam();
//        return MsgProducer.reportReversePayInfo()
//    }

    //    public static boolean reportReversePayInfo(){
    //        ReportReversePayInfoParam param = new ReportReversePayInfoParam();
    //        return MsgProducer.reportReversePayInfo()
    //    }
    private class MyLogHandler : AbstractLogHandler() {
        override fun handlerLog(msg: String, level: ClientConstants.LogLevel) {
            if (level.value() >= NettyClientProperties.logLevel) {
                logFile("lyy log：$msg")
            }
        }
    }


    private fun uuid(): String {
        return UUID.randomUUID().toString()
    }
}